package com.yunshipei.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;

import com.yunshipei.base.BaseActivity;
import com.yunshipei.base.BaseIntentBuilder;
import com.yunshipei.common.Globals;
import com.yunshipei.common.wedigt.YspTitleBar;
import com.yunshipei.enterplorer.R;
import com.yunshipei.enterplorer.browser.BaseHomeWebViewClient;
import com.yunshipei.enterplorer.browser.HomeWebChromeClient;
import com.yunshipei.enterplorer.browser.HomeWebView;
import com.yunshipei.inter.JsInterface.HomeExtendJsObject;

public class HomeExtendActivity extends BaseActivity {

    private static final String EXTRA_TITLE = "com.enterplorer.home_extend_title";
    private static final String EXTRA_URL = "com.enterplorer.home_extend_url";


    private HomeWebView mWebView;

    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_extend);
        YspTitleBar mTitleBar = (YspTitleBar) findViewById(R.id.et_home_extend_title);
        assert mTitleBar != null;
        mTitleBar.setTitle(getIntent().getStringExtra(EXTRA_TITLE));
        String url = getIntent().getStringExtra(EXTRA_URL);
        mWebView = (HomeWebView) findViewById(R.id.wv_home_extend);
        mWebView.setWebViewClient(new BaseHomeWebViewClient(this, mWebView));
        mWebView.setWebChromeClient(new HomeWebChromeClient(mWebView));
        mWebView.addJavascriptInterface(new HomeExtendJsObject(), Globals.JS_OBJECT_ENTERPLORER_HOME_EXTEND);
        mWebView.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.stopLoading();
            mWebView.onDestroy();
            mWebView = null;
        }
    }

    public static final class HomeExtendIntentBuilder extends BaseIntentBuilder {

        public HomeExtendIntentBuilder(Context context) {
            super(context);
        }

        public HomeExtendIntentBuilder setTitle(String title) {
            getIntent().putExtra(EXTRA_TITLE, title);
            return this;
        }

        public HomeExtendIntentBuilder setUrl(String url) {
            getIntent().putExtra(EXTRA_URL, url);
            return this;
        }

        @Override
        protected Class<?> getTargetClazz() {
            return HomeExtendActivity.class;
        }
    }
}
